package edu.stthomas.seis770.visitor;

import COM.hilbertinc.xml.AbstractReflectiveVisitor;
import com.ibm.xml.parser.Parser;
import com.ibm.xml.parser.TXDocument;

import java.io.FileInputStream;
import java.io.FileNotFoundException;

/**
 * Helper class to load xml document from a file and serves as entry point to ReflectiveHTMLVisitor
 * (Exercise part 3)
 */
public class XMLDomTraverser {

    private TXDocument xmlDocument = null;

    protected TXDocument getXmlDocument() {
        return xmlDocument;
    }

    /**
     * Initialize XMLDomTraverser object to traverse given fileName
     *
     * @param fileName file name of xml to traverse
     */
    public XMLDomTraverser(String fileName) {
        try {
            FileInputStream inputStream = new FileInputStream(fileName);
            Parser xmlDocumentParser = new Parser("xml document parser");
            xmlDocument = xmlDocumentParser.readStream(inputStream);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

    }

    public void traverse(AbstractReflectiveVisitor visitor) {
        TXDocument xmlDocument = getXmlDocument();
        if (xmlDocument == null || visitor == null) {
            return;
        }
        visitor.dispatch(xmlDocument);
    }
}
